Trò chơi đua xe động vật trong UNITY Engine
114.698 lượt xem;
- BoardLevel.cs
- LevelScreen /
- AnimalRacing /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections.Generic;
3
4 public class BoardLevel : MonoBehaviour {
5
6 public BitmapFont font;
7 public GameObject prefabs;
8 public List<Sprite> bgSprites;
9
10 private string[] heads;
11 private string[] hs;
12
13 public void Awake()
14 {
15 font = new BitmapFont("Fonts/font_banchoi", "Fonts/font_banchoi_xml", gameObject);
16 }
17
18 public void Start () {
19 int levelUnlock = Data.getData(Data.KEY_LEVEL + Attr.currentWorld);
20
21 createHeadTexts(Attr.currentWorld);
22
23 float[] lxs = new float[] { -3, -1.5f, 0, 1.5f, 3, -3, -1.5f, 0, 1.5f, 3, -3, -1.5f, 0, 1.5f, 3 };
24 float[] lys = new float[] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1 };
25 for (int i = 0; i < 15; i++)
26 {
27 GameObject levelObject = (GameObject)Instantiate(prefabs);
28 levelObject.transform.parent = gameObject.transform;
29 levelObject.transform.localPosition = new Vector3(lxs[i], lys[i], -1);
30 FontLevel fontLevel = levelObject.GetComponent<FontLevel>();
31 fontLevel.setBoardLevel(this);
32
33 //fontLevel.setText("" + (i + 1));
34 fontLevel.setText(i < levelUnlock ? heads[i] : hs[i]);
35
36 fontLevel.setStar(Data.getData(Data.KEY_STAR + (Attr.currentWorld * 15 + i)), i < levelUnlock);
37
38 if (i < levelUnlock)
39 {
40 fontLevel.bgObject.GetComponent<SpriteRenderer>().sprite = bgSprites[Attr.currentWorld + 1];
41
42 if (i == levelUnlock - 1)
43 {
44 levelObject.AddComponent<Actor>().addAction(new ActionRepeat(ActionRepeat.FOREVER, new ActionSequence(
45 new ActionScaleTo(0.95f, 0.95f, 0.2f, Interpolation.sine),
46 new ActionScaleTo(1, 1, 0.2f, Interpolation.sine)
47 )));
48
49 }
50
51 addClickListener(fontLevel.bgObject, i);
52 }
53 else
54 fontLevel.bgObject.GetComponent<SpriteRenderer>().sprite = bgSprites[0];
55 }
56
57 gameObject.AddComponent<Actor>().addAction(new ActionSequence(
58 new ActionScaleTo(0, 0, 0),
59 new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut)));
60 }
61
62 private void addClickListener(GameObject bgObject, int levelIndex)
63 {
64 bgObject.AddComponent<InputProcessor>();
65 LevelClickListener clickListener = bgObject.AddComponent<LevelClickListener>();
66 clickListener.levelIndex = levelIndex;
67 clickListener.boardLevel = gameObject;
68 }
69
70 public void Update () {
71
72 }
73
74 public BitmapFont getFont()
75 {
76 return font;
77 }
78
79 private void createHeadTexts(int world_index)
80 {
81 switch (world_index)
82 {
83 case 0:
84 heads = new string[] { "1", "q", "3", "4", "5", "w", "7", "8", "9", "10", "e", "12", "13", "14", "15" };
85 hs = new string[] { "1", "a", "3", "4", "5", "s", "7", "8", "9", "10", "d", "12", "13", "14", "15" };
86 break;
87 case 1:
88 heads = new string[] { "1", "2", "3", "4", "5", "r", "7", "8", "9", "10", "t", "12", "13", "14", "15" };
89 hs = new string[] { "1", "2", "3", "4", "5", "f", "7", "8", "9", "10", "g", "12", "13", "14", "15" };
90 break;
91 case 2:
92 heads = new string[] { "1", "2", "3", "4", "5", "y", "7", "8", "9", "10", "u", "12", "13", "14", "15" };
93 hs = new string[] { "1", "2", "3", "4", "5", "h", "7", "8", "9", "10", "j", "12", "13", "14", "15" };
94 break;
95 case 3:
96 heads = new string[] { "1", "2", "3", "4", "5", "i", "7", "8", "9", "10", "o", "12", "13", "14", "15" };
97 hs = new string[] { "1", "2", "3", "4", "5", "k", "7", "8", "9", "10", "l", "12", "13", "14", "15" };
98 break;
99 }
100 }
101 }
2 using System.Collections.Generic;
3
4 public class BoardLevel : MonoBehaviour {
5
6 public BitmapFont font;
7 public GameObject prefabs;
8 public List<Sprite> bgSprites;
9
10 private string[] heads;
11 private string[] hs;
12
13 public void Awake()
14 {
15 font = new BitmapFont("Fonts/font_banchoi", "Fonts/font_banchoi_xml", gameObject);
16 }
17
18 public void Start () {
19 int levelUnlock = Data.getData(Data.KEY_LEVEL + Attr.currentWorld);
20
21 createHeadTexts(Attr.currentWorld);
22
23 float[] lxs = new float[] { -3, -1.5f, 0, 1.5f, 3, -3, -1.5f, 0, 1.5f, 3, -3, -1.5f, 0, 1.5f, 3 };
24 float[] lys = new float[] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1 };
25 for (int i = 0; i < 15; i++)
26 {
27 GameObject levelObject = (GameObject)Instantiate(prefabs);
28 levelObject.transform.parent = gameObject.transform;
29 levelObject.transform.localPosition = new Vector3(lxs[i], lys[i], -1);
30 FontLevel fontLevel = levelObject.GetComponent<FontLevel>();
31 fontLevel.setBoardLevel(this);
32
33 //fontLevel.setText("" + (i + 1));
34 fontLevel.setText(i < levelUnlock ? heads[i] : hs[i]);
35
36 fontLevel.setStar(Data.getData(Data.KEY_STAR + (Attr.currentWorld * 15 + i)), i < levelUnlock);
37
38 if (i < levelUnlock)
39 {
40 fontLevel.bgObject.GetComponent<SpriteRenderer>().sprite = bgSprites[Attr.currentWorld + 1];
41
42 if (i == levelUnlock - 1)
43 {
44 levelObject.AddComponent<Actor>().addAction(new ActionRepeat(ActionRepeat.FOREVER, new ActionSequence(
45 new ActionScaleTo(0.95f, 0.95f, 0.2f, Interpolation.sine),
46 new ActionScaleTo(1, 1, 0.2f, Interpolation.sine)
47 )));
48
49 }
50
51 addClickListener(fontLevel.bgObject, i);
52 }
53 else
54 fontLevel.bgObject.GetComponent<SpriteRenderer>().sprite = bgSprites[0];
55 }
56
57 gameObject.AddComponent<Actor>().addAction(new ActionSequence(
58 new ActionScaleTo(0, 0, 0),
59 new ActionScaleTo(1, 1, 0.5f, Interpolation.swingOut)));
60 }
61
62 private void addClickListener(GameObject bgObject, int levelIndex)
63 {
64 bgObject.AddComponent<InputProcessor>();
65 LevelClickListener clickListener = bgObject.AddComponent<LevelClickListener>();
66 clickListener.levelIndex = levelIndex;
67 clickListener.boardLevel = gameObject;
68 }
69
70 public void Update () {
71
72 }
73
74 public BitmapFont getFont()
75 {
76 return font;
77 }
78
79 private void createHeadTexts(int world_index)
80 {
81 switch (world_index)
82 {
83 case 0:
84 heads = new string[] { "1", "q", "3", "4", "5", "w", "7", "8", "9", "10", "e", "12", "13", "14", "15" };
85 hs = new string[] { "1", "a", "3", "4", "5", "s", "7", "8", "9", "10", "d", "12", "13", "14", "15" };
86 break;
87 case 1:
88 heads = new string[] { "1", "2", "3", "4", "5", "r", "7", "8", "9", "10", "t", "12", "13", "14", "15" };
89 hs = new string[] { "1", "2", "3", "4", "5", "f", "7", "8", "9", "10", "g", "12", "13", "14", "15" };
90 break;
91 case 2:
92 heads = new string[] { "1", "2", "3", "4", "5", "y", "7", "8", "9", "10", "u", "12", "13", "14", "15" };
93 hs = new string[] { "1", "2", "3", "4", "5", "h", "7", "8", "9", "10", "j", "12", "13", "14", "15" };
94 break;
95 case 3:
96 heads = new string[] { "1", "2", "3", "4", "5", "i", "7", "8", "9", "10", "o", "12", "13", "14", "15" };
97 hs = new string[] { "1", "2", "3", "4", "5", "k", "7", "8", "9", "10", "l", "12", "13", "14", "15" };
98 break;
99 }
100 }
101 }
fontLevel.setText("" + (i + 1));